package com.cindata

import grails.transaction.Transactional
import org.springframework.security.access.annotation.Secured

import grails.converters.JSON
import java.text.SimpleDateFormat
import org.grails.web.util.WebUtils

import static org.springframework.http.HttpStatus.*

@Secured(['isAuthenticated()'])
@Transactional
class WorkflowController
{

    static allowedMethods = [save: "POST", update: "PUT", delete: "DELETE"]

    def index(Integer max)
    {
        params.max = Math.min(params.max ? params.max.toInteger() : 10, 100)
        params.offset = params.offset ? params.offset.toInteger() : 0

        def sort = params['sort']
        def order = params['order']
        def name = params['name']
        def type = params['type']
        def sql = " from Workflow o where 1=1  "
        if (name)
        {
            sql += " and o.name like '%${name}%'"
        }
        if (type)
        {
            sql += " and o.type.id = ${type}"
        }
        def countSql = " select count(o.id) " + sql
        if (order && sort)
        {
            sql += " order by o.${sort} ${order}"
        }
        else
        {
            sql += "order by o.name desc "
        }
        def workflowList = Workflow.executeQuery(sql, [max: params.max, offset: params.offset])
        def workflowCount = Workflow.executeQuery(countSql)
        respond workflowList, model: [workflowCount: workflowCount[0], params: params]
    }

    def show(Workflow workflow)
    {
        respond workflow
    }

    def create()
    {
        respond new Workflow(params)
    }

    @Transactional
    def save(Workflow workflow)
    {
        if (workflow == null)
        {
            transactionStatus.setRollbackOnly()
            notFound()
            return
        }

        if (workflow.hasErrors())
        {
            transactionStatus.setRollbackOnly()
            respond workflow.errors, view: 'create'
            return
        }

        workflow.save flush: true

        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.created.message', args: [message(code: 'workflow.label', default: 'Workflow'), workflow.id])
                redirect workflow
            }
            '*' { respond workflow, [status: CREATED] }
        }
    }

    def edit(Workflow workflow)
    {
        respond workflow
    }

    @Transactional
    def update(Workflow workflow)
    {
        if (workflow == null)
        {
            transactionStatus.setRollbackOnly()
            notFound()
            return
        }

        if (workflow.hasErrors())
        {
            transactionStatus.setRollbackOnly()
            respond workflow.errors, view: 'edit'
            return
        }

        workflow.save flush: true

        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.updated.message', args: [message(code: 'workflow.label', default: 'Workflow'), workflow.id])
                redirect workflow
            }
            '*' { respond workflow, [status: OK] }
        }
    }

    @Transactional
    def delete(Workflow workflow)
    {

        if (workflow == null)
        {
            transactionStatus.setRollbackOnly()
            notFound()
            return
        }

        workflow.delete flush: true

        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.deleted.message', args: [message(code: 'workflow.label', default: 'Workflow'), workflow.id])
                redirect action: "index", method: "GET"
            }
            '*' { render status: NO_CONTENT }
        }
    }

    protected void notFound()
    {
        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.not.found.message', args: [message(code: 'workflow.label', default: 'Workflow'), params.id])
                redirect action: "index", method: "GET"
            }
            '*' { render status: NOT_FOUND }
        }
    }
}
